<template>
  <div class="base-map-resource-content">
    <div class="query-content">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名称">
          <el-input v-model="formInline.userName" @change="onSubmit" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="formInline.userPhone" @change="onSubmit" />
        </el-form-item>
        <el-form-item label="用户角色">
          <el-select
            v-model="formInline.roleId"
            @change="onSubmit"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(item, index) in userRoleDict"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="状态">-->
        <!--          <el-select v-model="formInline.userState" placeholder="请选择">-->
        <!--            <el-option label="正常" value="1" />-->
        <!--            <el-option label="禁用" value="2" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" class="queryButton" @click="onSubmit"-->
        <!--            >查询</el-button-->
        <!--          >-->
        <!--          <el-button type="primary" class="resetButton" @click="onReset"-->
        <!--            >重置</el-button-->
        <!--          >-->
        <!--        </el-form-item>-->
      </el-form>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column align="center" label="序号" width="180">
          <template #default="scope">
            {{
              scope.$index +
              1 +
              formInline.pageSize * (formInline.pageIndex - 1)
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="登录名"
          prop="loginName"
          show-overflow-tooltip
        />
        <el-table-column label="用户名称" show-overflow-tooltip>
          <template #default="scope">
            <span class="userName">
              {{ scope.row.userName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="userPhone" label="用户手机号码" />
        <el-table-column
          label="用户角色"
          prop="roleName"
          show-overflow-tooltip
        />
        <el-table-column prop="areaName" label="用户所属区域" />
        <el-table-column
          prop="lastLoginTime"
          label="最后登录时间"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{
              formatDate(
                new Date(scope.row.lastLoginTime),
                'YYYY-MM-DD HH:mm:ss'
              )
            }}
          </template>
        </el-table-column>
        <!--          <el-table-column prop="userState" label="用户状态" show-overflow-tooltip>-->
        <!--            <template #default="scope">-->
        <!--              <span v-if="scope.row.userState == '1'">在线</span>-->
        <!--              <span v-else style="color: #ff455f">离线</span>-->
        <!--            </template>-->
        <!--          </el-table-column>-->
        <el-table-column label="操作">
          <template #default="scope">
            <el-popconfirm
              width="160px"
              confirm-button-text="确认"
              cancel-button-text="取消"
              icon-color="#626AEF"
              @confirm="handleUserOutline()"
              :title="outlineText"
            >
              <template #reference>
                <el-button
                  @click="handleEditClick(scope.row)"
                  text
                  type="text"
                  style="padding: 0"
                  v-if="currentUser?.hasPermission('hhgloffline')"
                >
                {{ currentUser?.getButtonName('hhgloffline') }}
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="slot, prev, pager, next"
      :page-size="formInline.pageSize"
      :current-page="formInline.pageIndex"
      :total="total"
      @current-change="handlePageChange"
    >
      <template #default> 共{{ total }}个会话 </template>
    </el-pagination>
    <!--    <el-dialog-->
    <!--      :visible.sync="dialogVisible"-->
    <!--      title="提示"-->
    <!--      align-center-->
    <!--      class="outline-dialog"-->
    <!--      width="30%"-->
    <!--    >-->
    <!--      <span-->
    <!--        >确认强制下线用户“{{ userInfo.userName-->
    <!--        }}{{ userInfo.userPhone ? userInfo.userPhone : '' }}”吗？</span-->
    <!--      >-->
    <!--      <template #footer>-->
    <!--        <div class="dialog-footer">-->
    <!--          <el-button @click="dialogVisible = false">取消</el-button>-->
    <!--          <el-button type="primary" @click="handleUserOutline">-->
    <!--            确认-->
    <!--          </el-button>-->
    <!--        </div>-->
    <!--      </template>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script setup lang="ts">
import { computed, getCurrentInstance, onMounted, reactive, ref } from 'vue'
import type { roleQueryParams } from '@/api/back-management/role/interface'
import { queryRoleList } from '@/api/back-management/role'
import { outlineUser, queryUserOnlineList } from '@/api/back-management/user'
import { formatDate } from '@/utils'
import { Message as ElMessage } from 'element-ui'
import type { OptionData } from '@/utils/type'
import type { UserInfoResponse } from '@/api/back-management/user/interface'

const vm = getCurrentInstance()

const store = vm?.proxy?.$store

const currentUser = store?.state?.user?.currentUser

const userRoleDict = ref<OptionData[]>([
  {
    label: '市级管理员',
    value: 'citymanager',
  },
  {
    label: '社区管理员',
    value: 'socialmanager',
  },
  {
    label: '街道管理员',
    value: 'streetmanager',
  },
  {
    label: '区建委',
    value: 'checkmanager',
  },
])

const formInline = reactive({
  pageIndex: 1,
  pageSize: 11,
  userName: '',
  roleId: '',
  userState: '',
  userPhone: '',
})

const total = ref(0)

const outlineText = computed(() => {
  return `确认强制下线用户“${userInfo.value.userName}${
    userInfo.value.userPhone ? userInfo.value.userPhone : ''
  }”吗？`
})

const dialogVisible = ref(false)

const getRoleList = () => {
  const params: roleQueryParams = {
    pageIndex: 1,
    pageSize: 999,
  }
  queryRoleList(params).then((res) => {
    if (res.success) {
      userRoleDict.value = []
      res.data.list.forEach((val) => {
        userRoleDict.value.push({
          label: val.roleName,
          value: val.pkid,
        })
      })
    }
  })
}

const tableData = ref<UserInfoResponse[]>([])
const userInfo = ref<Partial<UserInfoResponse>>({})
const getOnlineUserList = () => {
  queryUserOnlineList(formInline).then((res) => {
    if (res.success) {
      total.value = res.data.count
      tableData.value = res.data.list
    }
  })
}

onMounted(() => {
  getRoleList()
  getOnlineUserList()
})

const onSubmit = () => {
  getOnlineUserList()
}

const onReset = () => {
  formInline.pageIndex = 1
  formInline.userState = ''
  formInline.userPhone = ''
  formInline.userName = ''
  formInline.roleId = ''
  getOnlineUserList()
}

const handleEditClick = (item: UserInfoResponse) => {
  // Object.assign(userInfo, { ...item })
  // console.log(userInfo)
  // dialogVisible.value = true
  userInfo.value = { ...item }
}

const handleUserOutline = () => {
  outlineUser(userInfo.value.pkid!)
    .then((res) => {
      if (res.success) {
        dialogVisible.value = false
        onReset()
        ElMessage.success('下线用户成功！')
      }
    })
    .catch((e) => {
      ElMessage.error(e)
    })
}

const handlePageChange = (index: number) => {
  formInline.pageIndex = index
  getOnlineUserList()
}
</script>

<style lang="scss" scoped>
.base-map-resource-content {
  width: 100%;
  height: calc(100% - 27px);
  background-color: white;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  display: flex;
  flex-direction: column;

  .query-content {
    padding-left: 20px;
    width: 100%;
    line-height: 62px;
    border-bottom: 1px solid #eeeff5;

    :deep(.el-form) {
      .el-form-item {
        margin-bottom: 0;

        .resetButton {
          width: 80px;
          height: 32px;
          color: #28b1e4;
          background: #ffffff;
          border: 1px solid #28b1e4;
          border-radius: 2px;
        }

        .queryButton {
          width: 80px;
          height: 32px;
          background: #28b1e4;
          border-radius: 2px;
        }
      }
    }
  }

  .table {
    width: 100%;
    padding: 0 18px;
    overflow-y: auto;

    .userName {
      .item {
        margin-left: 6px;
      }
      :deep(.el-icon) {
        vertical-align: sub;

        .svg-icon {
          svg {
            width: 16px;
            height: 16px;
            color: #28b1e4;
          }
        }
      }
      :deep(.el-badge__content) {
        width: 12px;
        height: 13px;
        transform: translateY(85%) translateX(148%);
      }
    }

    :deep(.el-table__row--striped) {
      background: #f7f9fb;
    }
  }

  .el-pagination {
    margin-top: 25px;
    justify-content: end;
    margin-right: 18px;
    margin-bottom: 20px;
  }
}
</style>
